package com.baidu.mapapi;

import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

public abstract class ItemizedOverlay<Item extends OverlayItem> extends Overlay
  implements Overlay.Snappable
{
  private static int d = -1;
  private boolean a = true;
  private Drawable b;
  private Drawable c;
  private ItemizedOverlay<Item>.a e = null;
  private OnFocusChangeListener f = null;
  private int g = -1;
  private int h = -1;

  public ItemizedOverlay(Drawable paramDrawable)
  {
    this.b = paramDrawable;
    if (this.b != null)
    {
      this.c = new m().a(this.b);
      if (d == 0)
        a(this.b, b.c);
    }
  }

  private static Drawable a(Drawable paramDrawable, b paramb)
  {
    Drawable localDrawable;
    int k;
    int l;
    if ((paramDrawable == null) || (b.a == paramb))
    {
      localDrawable = null;
      return localDrawable;
    }
    Rect localRect1 = paramDrawable.getBounds();
    if ((localRect1.height() == 0) || (localRect1.width() == 0))
      paramDrawable.setBounds(0, 0, paramDrawable.getIntrinsicWidth(), paramDrawable.getIntrinsicHeight());
    Rect localRect2 = paramDrawable.getBounds();
    int i = localRect2.width() / 2;
    int j = -localRect2.height();
    if (paramb == b.b)
    {
      int i1 = j / 2;
      int i2 = -i1;
      k = i1;
      l = i2;
    }
    while (true)
    {
      while (true)
      {
        paramDrawable.setBounds(-i, k, i, l);
        localDrawable = paramDrawable;
      }
      k = j;
      l = 0;
    }
  }

  static Drawable a(ItemizedOverlay<?> paramItemizedOverlay)
  {
    return paramItemizedOverlay.b;
  }

  private void a(Canvas paramCanvas, MapView paramMapView, boolean paramBoolean, OverlayItem paramOverlayItem, int paramInt)
  {
    Drawable localDrawable = paramOverlayItem.getMarker(paramInt);
    if ((localDrawable == null) && (this.b == null));
    while (true)
    {
      boolean bool;
      Point localPoint;
      return;
      if (localDrawable != null)
        if (this.b == null)
        {
          bool = false;
          if (bool)
          {
            if (!(paramBoolean))
              break label138;
            localDrawable = this.c;
            this.c.setBounds(this.b.copyBounds());
            m.a(this.c, this.b);
          }
        }
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              localPoint = paramMapView.getProjection().toPixels(paramOverlayItem.getPoint(), null);
              if (!(bool))
                break label147;
              Overlay.a(paramCanvas, localDrawable, localPoint.x, localPoint.y);
            }
            bool = localDrawable.equals(this.b);
          }
          bool = true;
        }
        label138: localDrawable = this.b;
      }
      label147: Overlay.drawAt(paramCanvas, localDrawable, localPoint.x, localPoint.y, paramBoolean);
    }
  }

  protected static Drawable boundCenter(Drawable paramDrawable)
  {
    d = 2;
    return a(paramDrawable, b.b);
  }

  protected static Drawable boundCenterBottom(Drawable paramDrawable)
  {
    d = 1;
    return a(paramDrawable, b.c);
  }

  protected abstract Item createItem(int paramInt);

  public void draw(Canvas paramCanvas, MapView paramMapView, boolean paramBoolean)
  {
    int i = this.e.a();
    for (int j = 0; j < i; ++j)
    {
      int k = getIndexToDraw(j);
      if (k != this.h)
      {
        OverlayItem localOverlayItem2 = getItem(k);
        Point localPoint = paramMapView.getProjection().toPixels(localOverlayItem2.getPoint(), null);
        int l = paramMapView.getLeft();
        int i1 = paramMapView.getRight();
        int i2 = paramMapView.getTop();
        int i3 = paramMapView.getBottom();
        localPoint.x = (l + localPoint.x);
        localPoint.y = (i2 + localPoint.y);
        if ((localPoint.x >= l) && (localPoint.y >= i2) && (localPoint.x <= i1) && (localPoint.y <= i3))
          a(paramCanvas, paramMapView, paramBoolean, localOverlayItem2, 0);
      }
    }
    if (this.a)
    {
      OverlayItem localOverlayItem1 = getFocus();
      if (localOverlayItem1 != null)
        a(paramCanvas, paramMapView, false, localOverlayItem1, 4);
    }
  }

  public GeoPoint getCenter()
  {
    return getItem(getIndexToDraw(0)).getPoint();
  }

  public Item getFocus()
  {
    OverlayItem localOverlayItem = null;
    if (this.h != -1)
      localOverlayItem = this.e.a(this.h);
    return localOverlayItem;
  }

  protected int getIndexToDraw(int paramInt)
  {
    return this.e.b(paramInt);
  }

  public final Item getItem(int paramInt)
  {
    return this.e.a(paramInt);
  }

  public final int getLastFocusedIndex()
  {
    return this.g;
  }

  public int getLatSpanE6()
  {
    return this.e.a(true);
  }

  public int getLonSpanE6()
  {
    return this.e.a(false);
  }

  protected boolean hitTest(OverlayItem paramOverlayItem, Drawable paramDrawable, int paramInt1, int paramInt2)
  {
    Rect localRect = paramDrawable.getBounds();
    localRect.left -= 10;
    localRect.right = (10 + localRect.right);
    localRect.bottom = (10 + localRect.bottom);
    localRect.top -= 10;
    boolean bool = localRect.contains(paramInt1, paramInt2);
    localRect.left = (10 + localRect.left);
    localRect.right -= 10;
    localRect.bottom -= 10;
    localRect.top = (10 + localRect.top);
    return bool;
  }

  public Item nextFocus(boolean paramBoolean)
  {
    OverlayItem localOverlayItem;
    if (this.e.a() == 0)
      localOverlayItem = null;
    while (true)
    {
      label49: int i;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return localOverlayItem;
              if (this.g != -1)
                break label49;
              if (this.h != -1)
                break;
              localOverlayItem = null;
            }
            localOverlayItem = this.e.a(0);
          }
          if (this.h != -1)
            i = this.h;
          while (true)
          {
            while (true)
            {
              if (!(paramBoolean))
                break label107;
              if (i != this.e.a() - 1)
                break label93;
              localOverlayItem = null;
            }
            i = this.g;
          }
          label93: localOverlayItem = this.e.a(i + 1);
        }
        label107: if (i != 0)
          break;
        localOverlayItem = null;
      }
      localOverlayItem = this.e.a(i - 1);
    }
  }

  public boolean onKeyUp(int paramInt, KeyEvent paramKeyEvent, MapView paramMapView)
  {
    return false;
  }

  public boolean onSnapToItem(int paramInt1, int paramInt2, Point paramPoint, MapView paramMapView)
  {
    int i;
    if (this.e.a() > 0)
    {
      Point localPoint = new Point();
      OverlayItem localOverlayItem = this.e.a(0);
      paramMapView.getProjection().toPixels(localOverlayItem.getPoint(), localPoint);
      if (hitTest(localOverlayItem, localOverlayItem.mMarker, paramInt1 - localPoint.x, paramInt2 - localPoint.y))
      {
        paramPoint.x = localPoint.x;
        paramPoint.y = localPoint.y;
        i = 1;
      }
    }
    while (true)
    {
      return i;
      i = 0;
    }
  }

  protected boolean onTap(int paramInt)
  {
    if (paramInt != this.h)
      setFocus(getItem(paramInt));
    return false;
  }

  public boolean onTap(GeoPoint paramGeoPoint, MapView paramMapView)
  {
    return this.e.a(paramGeoPoint, paramMapView);
  }

  public boolean onTouchEvent(MotionEvent paramMotionEvent, MapView paramMapView)
  {
    return false;
  }

  public boolean onTrackballEvent(MotionEvent paramMotionEvent, MapView paramMapView)
  {
    return false;
  }

  protected final void populate()
  {
    this.e = new a(this);
    this.g = -1;
    this.h = -1;
  }

  public void setDrawFocusedItem(boolean paramBoolean)
  {
    this.a = paramBoolean;
  }

  public void setFocus(Item paramItem)
  {
    if ((paramItem != null) && (this.h == this.e.a(paramItem)));
    while (true)
    {
      do
      {
        do
        {
          while (true)
          {
            return;
            if ((paramItem != null) || (this.h == -1))
              break;
            if (this.f != null)
              this.f.onFocusChanged(this, paramItem);
            this.h = -1;
          }
          this.h = this.e.a(paramItem);
        }
        while (this.h == -1);
        setLastFocusedIndex(this.h);
      }
      while (this.f == null);
      this.f.onFocusChanged(this, paramItem);
    }
  }

  protected void setLastFocusedIndex(int paramInt)
  {
    this.g = paramInt;
  }

  public void setOnFocusChangeListener(OnFocusChangeListener paramOnFocusChangeListener)
  {
    this.f = paramOnFocusChangeListener;
  }

  public abstract int size();

  public static abstract interface OnFocusChangeListener
  {
    public abstract void onFocusChanged(ItemizedOverlay paramItemizedOverlay, OverlayItem paramOverlayItem);
  }

  class a
  implements Comparator<Integer>
  {
    private ArrayList<Item> b;
    private ArrayList<Integer> c;
    private ItemizedOverlay<Item> d;

    public a()
    {
      this.d = paramItemizedOverlay;
      int i = paramItemizedOverlay.size();
      this.b = new ArrayList(i);
      this.c = new ArrayList(i);
      for (int j = 0; j < i; ++j)
      {
        this.c.add(Integer.valueOf(j));
        OverlayItem localOverlayItem = paramItemizedOverlay.createItem(j);
        this.b.add(localOverlayItem);
      }
      Collections.sort(this.c, this);
    }

    private Point a(, Projection paramProjection, Point paramPoint)
    {
      Point localPoint = paramProjection.toPixels(paramOverlayItem.getPoint(), null);
      return new Point(paramPoint.x - localPoint.x, paramPoint.y - localPoint.y);
    }

    public final int a()
    {
      return this.b.size();
    }

    public final int a()
    {
      int j;
      int i = -1;
      if (paramItem != null)
        j = 0;
      while (true)
      {
        if (j >= this.b.size());
        while (true)
        {
          return i;
          if (!(paramItem.equals(this.b.get(j))))
            break;
          i = j;
        }
        ++j;
      }
    }

    public int a(, Integer paramInteger2)
    {
      int i;
      GeoPoint localGeoPoint1 = ((OverlayItem)this.b.get(paramInteger1.intValue())).getPoint();
      GeoPoint localGeoPoint2 = ((OverlayItem)this.b.get(paramInteger2.intValue())).getPoint();
      if (localGeoPoint1.getLatitudeE6() > localGeoPoint2.getLatitudeE6())
        i = -1;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return i;
              if (localGeoPoint1.getLatitudeE6() >= localGeoPoint2.getLatitudeE6())
                break;
              i = 1;
            }
            if (localGeoPoint1.getLongitudeE6() >= localGeoPoint2.getLongitudeE6())
              break;
            i = -1;
          }
          if (localGeoPoint1.getLongitudeE6() >= localGeoPoint2.getLongitudeE6())
            break;
          i = 0;
        }
        i = 1;
      }
    }

    public final int a()
    {
      Iterator localIterator;
      int i;
      int j;
      int k;
      int l;
      if (this.b.size() == 0)
        l = 0;
      while (true)
      {
        return l;
        localIterator = this.b.iterator();
        i = -2147483648;
        j = 2147483647;
        if (localIterator.hasNext())
          break;
        l = i - j;
      }
      GeoPoint localGeoPoint = ((OverlayItem)localIterator.next()).getPoint();
      if (paramBoolean)
        k = localGeoPoint.getLatitudeE6();
      while (true)
      {
        while (true)
        {
          do
            if (k > i)
              i = k;
          while (k >= j);
          j = k;
        }
        k = localGeoPoint.getLongitudeE6();
      }
    }

    public final Item a()
    {
      return ((OverlayItem)this.b.get(paramInt));
    }

    public final boolean a(, MapView paramMapView)
    {
      int k;
      boolean bool = false;
      Projection localProjection = paramMapView.getProjection();
      Point localPoint1 = localProjection.toPixels(paramGeoPoint, null);
      int i = this.b.size();
      int j = 0;
      if (j < i)
      {
        OverlayItem localOverlayItem = (OverlayItem)this.b.get(j);
        double d1 = -1.0D;
        Point localPoint2 = a(localOverlayItem, localProjection, localPoint1);
        Drawable localDrawable = localOverlayItem.mMarker;
        if (localDrawable == null)
          localDrawable = ItemizedOverlay.a(this.d);
        ItemizedOverlay localItemizedOverlay = this.d;
        int l = localPoint2.x;
        int i1 = localPoint2.y;
        if (localItemizedOverlay.hitTest(localOverlayItem, localDrawable, l, i1))
          d1 = localPoint2.x * localPoint2.x + localPoint2.y * localPoint2.y;
        if ((d1 >= 0D) && (d1 < 1.7976931348623157e+308D))
          k = j;
      }
      while (true)
      {
        if (-1 != k)
          bool = this.d.onTap(k);
        while (true)
        {
          while (true)
          {
            return bool;
            ++j;
          }
          this.d.setFocus(null);
        }
        k = -1;
      }
    }

    public final int b()
    {
      return ((Integer)this.c.get(paramInt)).intValue();
    }
  }

  static enum b
  {
    static
    {
      b[] arrayOfb = new b[3];
      arrayOfb[0] = a;
      arrayOfb[1] = b;
      arrayOfb[2] = c;
      d = arrayOfb;
    }
  }
}